package com.ticket.sass.admin.dto.output.order;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ticket.sass.admin.dto.output.pricecheme.PriceSchemeSimpOutput;
import com.ticket.sass.common.entity.tenant.OrderTicketEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author ywb
 * @date 2024-03-14 14:32
 */
@Data
@ApiModel
public class OrderTicketListOutput {
    @ApiModelProperty(value = "ID")
    private Long id;
    
    @ApiModelProperty(value = "订单ID")
    private Long orderId;
    
    @ApiModelProperty(value = "订单号")
    private String orderNo;
    
    @ApiModelProperty(value = "用户ID")
    private Long userId;
    
    @ApiModelProperty(value = "用户名")
    private String username;
    
    @ApiModelProperty(value = "手机号")
    private String cellphone;
    
    
    @ApiModelProperty(value = "场次信息")
    private SessionOutput session;
    
    @ApiModelProperty(value = "票信息")
    private List<TicketOutput> tickets;
    
    @ApiModelProperty(value = "取票码 json 格式")
    private Map<String, String> takeCode;
    
    @ApiModelProperty(value = "是否使用优惠券")
    private Boolean useCoupon;
    
    @ApiModelProperty(value = "优惠券信息")
    private List<CouponOutput> coupons;
    
    @ApiModelProperty(value = "票价（原价）")
    private BigDecimal ticketPrice;
    
    @ApiModelProperty(value = "支付价格")
    private BigDecimal payPrice;
    
    @ApiModelProperty(value = "订单备注")
    private String remark;
    
    @ApiModelProperty(value = "支付类型：1 微信，2 支付宝，3 会员卡")
    private Short payType;
    
    @ApiModelProperty(value = "支付状态：0 未支付，1 支付中，2 已支付，3 支付失败，4 退款中，5 已退款")
    private Short payStatus;
    
    @ApiModelProperty(value = "出票状态：0 未出票，1 出票中，2 出票成功，3 出票失败，4 退票中 5 已退票")
    private Short ticketStatus;
    
    @ApiModelProperty(value = "排期 id")
    private Long filmScheduleId;
    
    @ApiModelProperty(value = "支付时间")
    private LocalDateTime payTime;
    
    @ApiModelProperty(value = "下单时间")
    public LocalDateTime createdAt;
    
    @ApiModelProperty(value = "购票数量")
    private Integer ticketCount;
    
    @ApiModelProperty(value = "票价方案 id")
    private Long priceSchemeId;
    
    @ApiModelProperty(value = "票价方案 id")
    private PriceSchemeSimpOutput priceSchemes;
    
    @ApiModelProperty(value = "退款备注")
    private String refundRemark;
    
    public OrderTicketListOutput() {
    }
    
    public OrderTicketListOutput(OrderTicketEntity entity) {
        this.id = entity.getId();
        this.orderNo = entity.getOrderNo();
        this.orderId = entity.getOrderId();
        
        this.session = new SessionOutput(entity.getSessionStartTime(), entity.getSessionEndTime(), entity.getFilmName(), entity.getHallName(), entity.getFilmLang(), entity.getFilmVersion());
        this.useCoupon = entity.getUseCoupon();
        this.coupons = new ArrayList<>();
        this.tickets = new ArrayList<>();
        this.userId = entity.getUserId();
        this.takeCode = toMap(entity.getTakeCode());
        
        this.ticketPrice = entity.getTicketPrice();
        this.payPrice = entity.getPayPrice();
        this.remark = entity.getRemark();
        this.payType = entity.getPayType();
        this.payStatus = entity.getPayStatus();
        this.ticketStatus =  entity.getTicketStatus() != null ? entity.getTicketStatus().getValue() : 0;
        this.filmScheduleId = entity.getFilmScheduleId();
        this.payTime = entity.getPayTime();
        this.createdAt = entity.getOrderCreatedAt();
        this.ticketCount = entity.getTicketCount();
        this.priceSchemeId = entity.getPriceSchemeId();
    }
    
    public List<CouponOutput> getCoupons() {
        if (Objects.isNull(coupons)){
            return new ArrayList<>();
        }
        
        return coupons;
    }
    
    private Map<String, String> toMap(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> takeCode = new HashMap<>();
        try {
            List<Map<String, String>> list = objectMapper.readValue(json, new TypeReference<List<Map<String, String>>>() {});
            for (Map<String, String> map : list) {
                takeCode.put(map.get("name"), map.get("value"));
            }
        } catch (
            IOException e) {
            e.printStackTrace();
        }
        return takeCode;
    }
    
    
}
